/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var fourSum = function(nums, target) {
    nums.sort((v1,v2)=>v1-v2);
    if(nums.length<4)return [];
    //console.log(nums);
    let result = new Map();
    let length = nums.length;
    for (let n=0;n<length-3;n++) {

        if(nums[n]+nums[n+1]+nums[n+2]+nums[n+3]>target)break;

        for(let i=n+1;i<length-2;i++){

            let j = i+1;
            let k = length-1;

            while(j<k){
                let sum = nums[n]+nums[i]+nums[j]+nums[k];
                if(sum>target){
                    k--;
                }
                else if(sum<target){
                    j++;
                }
                else{
                    let key = [nums[n],nums[i],nums[j],nums[k]].join();
                    result.set(key, [nums[n],nums[i],nums[j],nums[k]]);
                    j++;
                    k--;
                }
            }
        }
    }
    return Array.from(result.values());
};




console.log(fourSum([1, 0, -1, 0, -2, 2],0));